import fileSearch from "@/views/home/api/fileSearch";
import contentSearch from "@/views/home/api/contentSearch";
import toDirectory from "@/views/home/api/toDirectory";
import batchDelete from "@/views/home/api/batchDelete";
import TitleModal from "@/views/home/modal/TitleModal";
import AddFileModal from "@/views/home/modal/AddFileModal";
import MoveFileModal from "@/views/home/modal/MoveFileModal";
import LoginModal from "@/views/home/modal/LoginModal";
import FileTypeIcon from "@/components/icon/FileTypeIcon";

export default {
    name: "Home",
    components: {TitleModal, AddFileModal, MoveFileModal, LoginModal, FileTypeIcon},
    data() {
        return {
            keyword: "",
            searchType: "file",
            searchResultCollapse: [],
            route: [],
            selectFiles: [],
            files: [],
            titleModal: {
                show: false,
                file: {}
            },
            addFileModal: {
                show: false
            },
            moveFileModal: {
                show: false
            },
            loginModal: {
                show: false
            }
        }
    },
    methods: {
        async querySearchAsync(queryString, cb) {
            if (!queryString) {
                return;
            }
            let results;
            if (this.isSearchFile) {
                results = await fileSearch(queryString);
            } else {
                results = await contentSearch(queryString);
            }
            if (!results.success) {
                return;
            }

            cb(results.data);
        },
        handleSelect(file) {
            if (file.directoryFlag) {
                //导航
                this.toDirectory(file.id);
            } else {
                //打开文件
                this.openFile(file.id, file.title, file.fileType);
            }
        },
        handleHighlightClick(fileId, title, fileType, subNo) {
            this.openFile(fileId, title, fileType, subNo);
        },
        backDirectory() {
            let length = this.route.length;
            if (length < 2) {
                this.$message.warning("无路可退啦！")
                return;
            }
            let dir = this.route[this.route.length - 2];
            this.toDirectory(dir.id);
        },
        async toDirectory(id) {
            let response = await toDirectory(id);
            if (!response.success) {
                this.$message.error(response.message);
            }
            this.files = response.data.children;
            //面包屑更新
            this.route = [];
            let directory = response.data.route;
            while (directory) {
                let dir = {
                    id: directory.id,
                    title: directory.title
                };
                this.route.push(dir);
                directory = directory.child;
            }
            //取消选址
            this.selectFiles = [];
        },
        refreshCurrent() {
            let dir = this.route[this.route.length - 1];
            this.toDirectory(dir.id);
        },
        openFile(id, filename, fileType, subNo) {
            const href = this.$router.resolve({
                path:'/' + fileType,
                query:{
                    id,
                    filename,
                    fileType,
                    subNo
                }
            })
            console.log("open: " + href.href);
            window.open(href.href, '_blank')
        },
        async del() {
            let response = await batchDelete(this.selectFiles);
            if (response.success) {
                this.refreshCurrent();
            }
        },
        showTitleModal4Add() {
            this.titleModal.file = {};
            this.titleModal.show = true;
        },
        showTitleModal4Edit() {
            if (this.selectFiles.length === 0) {
                this.$message.warning("请先选择文件")
                return;
            }
            if (this.selectFiles.length > 1) {
                this.$message.warning("只能修改一个文件")
                return;
            }
            this.titleModal.file = this.files.filter(file => file.id === this.selectFiles[0])[0];
            this.titleModal.show = true;
        },
        showAddFileModal() {
            this.addFileModal.show = true;
        },
        showMoveFileModal() {
            if (this.selectFiles.length === 0) {
                this.$message.warning("请先选择文件")
                return;
            }
            this.moveFileModal.show = true;
        },
        showLogin() {
            this.loginModal.show = true;
        },
        hideLogin() {
            window.localStorage.setItem('showLogin', 'false');
            this.loginModal.show = false;
        }
    },
    computed: {
        isSearchFile() {
            return this.searchType === "file";
        }
    },
    mounted() {
        this.toDirectory(0);

        setInterval(() => {
            let state = window.localStorage.getItem('showLogin');
            if (state !== "false") {
                this.showLogin();
            }
        }, 1000)
    }
}